package com.gmu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.pojo.TblDoctor;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblTeacher;
import com.gmu.pojo.TblUser;
import com.gmu.service.TblDoctorService;
import com.gmu.service.TblUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医护人员信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@RestController
@Api(tags = "医生信息表")
@RequestMapping("/gmu/tbl-doctor")
public class TblDoctorController {

    @Autowired
    private TblDoctorService doctorService;

    @Resource
    private TblUserService userService;

    //查询所有医生信息
    @GetMapping("listAll")
    @ApiOperation("所有医生信息")
    public ResResult listAll(){
        List<TblDoctor> doctorList = doctorService.list(null);
        return new ResResult(200,"查询所有医生信息成功",doctorList);
    }

    //添加或修改医生的方法
    @PostMapping("addDoctor")
    @ApiOperation("添加和修改医生信息")
    public ResResult addDoctor(@RequestBody TblDoctor tblDoctor){
        QueryWrapper<TblUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id_number", tblDoctor.getIdNumber());
        TblUser tblUser = userService.getOne(wrapper);
        if(tblUser==null){
            return new ResResult(201,"该身份证暂未注册，请先注册");
        }
        Integer userId = tblUser.getUserId();
        tblDoctor.setUserId(userId);
        boolean save = doctorService.save(tblDoctor);
        if(save){
            return new ResResult(200,"执行成功");
        }else{
            return new ResResult(201,"执行失败");
        }
    }

    //根据id查询医生信息
    //用于修改前的数据回显
    @GetMapping("byIdDoctor/{doctorId}")
    @ApiOperation("根据id查询医生信息")
    public ResResult byIdDoctor(@PathVariable Integer doctorId){
        TblDoctor doctorById = doctorService.getById(doctorId);
        return new ResResult(200,"查询成功",doctorById);
    }

    //根据id删除医生信息
    @DeleteMapping("deleteDoctor/{doctorId}")
    @ApiOperation("删除医生信息")
    public ResResult deleteDoctor(@PathVariable Integer doctorId){
        Integer userId = doctorService.getById(doctorId).getUserId();
        boolean removeById = doctorService.removeById(doctorId);
        userService.removeById(userId);
        if(removeById){
            return new ResResult(200,"删除成功");
        }else{
            return new ResResult(201,"删除失败");
        }
    }

    // 分页查询医生的方法
    @ApiOperation("分页查询")
    @GetMapping("pageDoctor/{current}/{limit}")
    public ResResult pageListDoctor(@PathVariable long current,
                                     @PathVariable long limit) {
        //创建page对象
        Page<TblDoctor> pageDoctor = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageTeacher对象里面
        doctorService.page(pageDoctor, null);

        long total = pageDoctor.getTotal();//总记录数
        List<TblDoctor> records = pageDoctor.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return new ResResult(200,map);
    }

}

